#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

char *strstrip(const char *in_str, char *out_str, int len)
{
    const char *last  = NULL;
    const char *begin = NULL;

    if (in_str == NULL || out_str == NULL) 
	return NULL;

    while (isspace ((int)*in_str) && *in_str) 
	in_str++;
    begin = in_str;

    for (; *in_str; ++in_str)
    {
	if (isspace((int)*in_str) == 0)
	    last = in_str;
    }
    int l = last - begin + 1;
    if (l < len)
	len = l;
    strncpy (out_str, begin, len);
    return out_str ;
}
char *strstrip_all (const char *in_str, char *out_str, int len)
{
    if (in_str == NULL || out_str == NULL)
	return NULL;

    int i;
    char *s = out_str;
    for (i = 0; *in_str && i < len; ++in_str)
    {
	if (isspace((int)*in_str) == 0)
	    *s++ = *in_str;
    }
    return out_str;
}
int get_parent (const char *str, char *parent)
{
    if (str == 0) return 0;
    char *pos = strrchr (str, '.');
    char ts[256] = {0};
    int l = 0;
    l = atoi (strrchr (str, ':') + 1);
    printf ("%s: %d\n", str, l);
    if (pos) // grade
    {
	strncpy (parent, str, pos - str);
	//printf ("%s: %d\n", parent, l);
	return l + get_parent (parent, ts);
    }
    return l;
}

int main ()
{
    char str[] = "std out    |	fi  les | ne\ntwork\n\n";
    char *saveptr = 0;
    char *token = 0;
    char *str1 = 0;
    char ostr[64];
    printf ("include %d |\n", strspn ("caebbage", "abc"));
    memset(ostr, '\0', sizeof(ostr));
    for (str1 = str; ; str1 = 0)
    {
	token = strtok(str1, "|");
	if (token == NULL)
	    break;
	printf ("[%s]->[%s]\n", token, strstrip_all(token, ostr, sizeof(ostr) - 1));
	printf (ostr);
    }
    printf (str);
    char *arr[]  = { "abc", "defg" };
    printf ("size = %d\n", sizeof(arr)/sizeof(arr[0]));
    
    char istr[] = "4s";
    printf ("istr = %d\n", atoi (istr));

    char s[] = "cui.shao.wei";
    printf ("sizeof s = %d\n", sizeof(s));
    char *pos = strrchr(s, '.');
    char s1[32] = {0};
    strncpy (s1, s, pos - s);
    printf ("%s\n", s1);
    //
    char root[] = "root:1.http:2.server:3.log:4.trace:5";
    int in_ll = 0;
    int llevel = get_parent (root, ostr);
    printf ("llevel = %d\n", llevel);
}
